<?php

namespace app\models\task;

use app\models\user\User;
use Yii;

/**
 * This is the model class for table "task_list".
 *
 * @property int $id
 * @property int $user_id
 * @property int $task_id
 * @property string $create_time
 * @property string $update_time
 * @property int $status
 * @property string $reject
 */
class TaskList extends \yii\db\ActiveRecord
{

    /**
     * status list
     * @var int[]
     */
    public static $statusMapping = [
        'ALL'        => -1,
        'WAIT'       => 1,
        'CHECK'       => 2,
        'FINISH'     => 3,
        'REJECT'     => 4,
    ];

    /**
     * status display list
     * @var int[]
     */
    public static $statusMappingDisplay = [
        '全部'        => -1,
        '待完成'       => 1,
        '审核中'       => 2,
        '已完成'       => 3,
        '已驳回'       => 4,
    ];

    /**
     * Status list
     *
     * @return array
     */
    public static function statusList()
    {
        $list = [];
        foreach (self::$statusMappingDisplay as $key => $value) {
            $list[] = [
                'label' => $key,
                'value' => $value
            ];
        }

        return $list;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'task_list';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'task_id', 'status'], 'integer'],
            [['create_time', 'update_time'], 'safe'],

            [['reject'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'task_id' => 'Task ID',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'status' => 'Status',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getTask()
    {
        return $this->hasOne(Task::class, ['id' => 'task_id']);
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id']);
    }

    public function getImages()
    {
        return $this->hasMany(TaskImage::class, ['task_list_id' => 'id']);
    }
}
